package com.plugin.view

import com.map.interfaces.OnInfoWindowClick
import com.map.interfaces.OnPolygonClick
import com.map.interfaces.Point
import com.map.tiandimap.MapView
import com.map.utils.TransferOfAxesUtil
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel

/**
 * Create： zhangjunfei 1/25/21 1:14 PM
 * 描述：地图控制类
 */
class MapViewControl(
    private val mapView: MapView,
    private val zoomLevel: Int
) {
    init {
        initMap()
    }

    /**
     * 清空地图上的mark点
     */
    private fun clearMarkerPoint() {
        mapView.clearAllLayer()
    }

    /**
     * 清除绘制的面
     */
    private fun clearLineAreas() {
        mapView.clearAllLayer()
    }

    /**
     * 设置地图缩放级别
     */
    private fun setZoomLevel(zoom: Int) {
        mapView.zoomTo(zoom)
    }

    /**
     * 在地图上画面
     */
    private fun addLineArea(points: MutableList<Point>, color: String, tag: String) {
        mapView.drawArea(
            points = points,
            color = color,
            fillColor = color,
            fillOpacity = 0.5f,
            tag = tag
        )
    }

    /**
     * 地图初始化
     */
    private fun initMap() {
        setZoomLevel(zoomLevel)
    }

    //------------------flutter接口定义------------------
    /**
     * 展示当前位置坐标点
     */
    fun showLocation(result: MethodChannel.Result) {
        //设置地图的中心点,为当前位置
        mapView.location()
        result.success(true)
    }

    /**
     * 设置地图的中心点
     */
    fun setMapCenter(call: MethodCall, result: MethodChannel.Result) {
        val params: HashMap<*, *> = call.arguments as HashMap<*, *>

        val pointList = TransferOfAxesUtil.gcj02ToWgs84(
            params["longitude"] as Double,
            params["latitude"] as Double
        )
        val point = Point(lng = pointList[0], lat = pointList[1])

        //设置地图的中心点,为当前位置
        mapView.setCenter(
            lng = point.lng,
            lat = point.lat,
            zoom = params["zoom"] as Int
        )
        result.success(true)
    }

    /**
     * 设置地图的缩放级别
     */
    fun zoomToLevel(call: MethodCall, result: MethodChannel.Result) {
        val params: HashMap<*, *> = call.arguments as HashMap<*, *>
        setZoomLevel(params["level"] as Int)
        result.success(true)
    }

    /**
     * 地图上添加一个封闭的面
     */
    fun addArea(call: MethodCall, result: MethodChannel.Result) {

        val params: HashMap<*, *> = call.arguments as HashMap<*, *>
        val originalPoints: List<*> = params["points"] as List<*>
        val color: String = params["color"] as String
        val tag: String = params["tag"] as String

        val points = mutableListOf<Point>()

        originalPoints.forEach {
            val mark = it as HashMap<*, *>

            val pointList = TransferOfAxesUtil.gcj02ToWgs84(
                mark["longitude"] as Double,
                mark["latitude"] as Double
            )

            val point = Point(lng = pointList[0], lat = pointList[1])
            points.add(point)
        }

        addLineArea(points, color, tag)

        result.success(true)
    }

    /**
     * 清除锚点
     */
    fun clearMarkers(result: MethodChannel.Result) {
        clearMarkerPoint()
        result.success(true)
    }

    /**
     * 清除绘制的面
     */
    fun clearAreas(result: MethodChannel.Result) {
        clearLineAreas()
        result.success(true)
    }

    /**
     * 添加infoWindow
     */
    fun addInfoWindow(call: MethodCall, result: MethodChannel.Result) {
        val params: HashMap<*, *> = call.arguments as HashMap<*, *>
        val lng: Double = params["longitude"] as Double
        val lat: Double = params["latitude"] as Double
        val pointList = TransferOfAxesUtil.gcj02ToWgs84(
            lng,
            lat,
        )

        val msg: String = params["msg"] as String
        val tag: String = params["tag"] as String
        val desc: String = params["desc"] as String
        mapView.addInfoWindow(pointList[0], pointList[1], msg, desc, tag)
        result.success(true)
    }

    /**
     * 添加面的点击事件监听
     */
    fun addOnPolygonClickListener(methodChannel: MethodChannel, result: MethodChannel.Result) {
        mapView.addOnPolygonClickListener(object : OnPolygonClick {
            override fun onClick(tag: String) {
                methodChannel.invokeMethod("onPolygonClick", tag)
            }
        })
        result.success(true)
    }

    /**
     * 添加infoWindow的点击事件
     */
    fun addOnInfoWindowClickListener(methodChannel: MethodChannel, result: MethodChannel.Result) {
        mapView.addOnInfoWindowClickListener(object : OnInfoWindowClick {
            override fun onClick(tag: String) {
                methodChannel.invokeMethod("onInfoWindowClick", tag)
            }
        })
        result.success(true)
    }
}